/*
 * Game.cpp
 *
 *  Created on: 15/07/2008
 *      Author: Samuk
 */

#include "StickBombers.h"

class StickBombersCleaner {
public:

	~StickBombersCleaner() {
		StickBombers::Destroy();
	}

} GameCleanerInst;

StickBombers* StickBombers::_instance = NULL;
int StickBombers::_refCount = 0;

void StickBombers::ConfiguraMapa(char *Mapa) {
	MapAtivo = new SBMap();
	MapAtivo->Init(Mapa);
}

void StickBombers::ConfiguraPlayer() {
	EntPlayer = new Player();
	if (EntPlayer->Init(0, 0, MapAtivo->Altura, MapAtivo->Largura) == false) {
		fprintf(stderr, "Erro ao carregar Player: %s", SDL_GetError());
		exit(1);
	}
}

void StickBombers::ExecutaLimpeza() {
	for (Uint32 i = 0; i < CEntity::EntityList->size(); i++) {
		if ((*CEntity::EntityList)[i] == NULL)
			continue;
		if ((*CEntity::EntityList)[i]->IsTrash()) {
			(*CEntity::EntityList)[i]->OnCleanup();
			delete (*CEntity::EntityList)[i];
			(*CEntity::EntityList)[i] = NULL;
			continue;
		}
	}
}

void StickBombers::RemoveEntidades() {
	for (Uint32 i = 0; i < CEntity::EntityList->size(); i++) {
		if (!(*CEntity::EntityList)[i])
			continue;
		(*CEntity::EntityList)[i]->OnCleanup();
		delete (*CEntity::EntityList)[i];
		(*CEntity::EntityList)[i] = NULL;
	}
	delete CEntity::EntityList;
}

void StickBombers::RemoveSurfaces() {
	std::map<std::string, SDL_Surface*>::iterator Iter;
	for (Iter = CSurface::GetSurfaces()->begin(); Iter
			!= CSurface::GetSurfaces()->end(); Iter++) {
		SDL_FreeSurface(Iter->second);
		delete Iter->second;
	}
}

StickBombers::StickBombers() :
	Rodando(true) {
	SurfDisplay = NULL;
	MapAtivo = NULL;
	EntPlayer = NULL;
}

StickBombers::~StickBombers() {
	//
}

StickBombers* StickBombers::Instance() {
	if (_instance == 0) { // if first time ...
		_instance = new StickBombers(); // ... create a new 'solo' instance
	}
	_refCount++; //increase reference counter
	return _instance;
}

void StickBombers::Release() {
	if (--_refCount < 1) {
		Destroy();
	}
}

void StickBombers::Destroy() {
	if (_instance != NULL) {
		delete (_instance);
		_instance = NULL;
	}
}

bool StickBombers::OnInitialize() {
	UtilSDL::ConfiguraSDL(SDL_INIT_EVERYTHING);

	UtilSDL::ConfiguraTitulo(CParametros::GetTituloJanela(),
			CParametros::GetIcone());
	SurfDisplay = UtilSDL::ConfiguraJanela(CParametros::GetLarguraJanela(),
			CParametros::GetAlturaJanela(),
			CParametros::GetBPP(),
			CParametros::GetJanelaFlags());
	ConfiguraMapa(CParametros::GetMapa());
	ConfiguraPlayer();

	return true;
}

void StickBombers::OnEvent(SDL_Event* event) {
	CEvent::OnEvent(event);
	EntPlayer->OnEvent(event);
}

void StickBombers::OnLoop() {
 	for (Uint32 i = 0; i < CEntity::EntityList->size(); i++) {
		if ((*CEntity::EntityList)[i] == NULL)
			continue;
		(*CEntity::EntityList)[i]->OnLoop();
	}
}

void StickBombers::OnRender() {
	MapAtivo->OnRender(SurfDisplay);

	for (Uint32 i = 0; i < CEntity::EntityList->size(); i++) {
		if ((*CEntity::EntityList)[i] == NULL)
			continue;
		if ((*CEntity::EntityList)[i]->IsTrash())
			continue;

		(*CEntity::EntityList)[i]->OnRender(SurfDisplay);
	}

	SDL_Flip(SurfDisplay);
}

void StickBombers::OnCleanup() {
	RemoveEntidades();
	RemoveSurfaces();

	SDL_FreeSurface(SurfDisplay);
	SDL_Quit();

	delete SurfDisplay;
}

void StickBombers::OnExit() {
	Rodando = false;
}
